var Http = new Object();
Http._prevRequest = function(alias, param, requestType) {
	var that = this;
	switch(requestType) {
		case 'http':
			that.httpRequest(param, alias)
			break;
		case 'upload':
			that.uploadRequest(param, alias)
			break;
		default:
			break;
	}
}

Http.uploadRequest = function(param, url) {
	var that = this;
	var _success = param.success;
	var task = plus.uploader.createUpload(url, {
			method: 'POST',
			blocksize: 0
		}, function(res, status) {
			if(status == 200) {
				var result = JSON.parse(res.responseText.trim());
				if(result.code == 1) {
					_success(result);
				} else {
					mui.toast('上传失败，' + result.message);
					plus.nativeUI.closeWaiting();
				}
			} else {
				mui.toast('上传失败，服务器错误：' + status);
				plus.nativeUI.closeWaiting();
			}
		}

	);
	task.addFile(param.filePath, {
		key: 'file'
	});
	task.setRequestHeader('sign', param.headers.sign);
	task.setRequestHeader('type', param.headers.type);
	task.setRequestHeader('did', param.headers.did);
	task.setRequestHeader('language', param.headers.language);
	task.setRequestHeader('token', param.userToken);
	task.setRequestHeader('version', param.headers.version);
	task.addEventListener("statechanged", that.onStateChanged, false);
	task.start()

}
Http.onStateChanged = function(upload, status) {
	plus.nativeUI.showWaiting('正在上传');
	if(upload.state == 4 && status == 200) {
		plus.nativeUI.closeWaiting()
	}
}

Http.httpRequest = function(param, url) {
	var _success = param.success;
	//				console.log('----------')
	//				console.log(url)
	//				console.log(JSON.stringify(param.data))
	//				console.log('请求头：' + JSON.stringify(param.headers))
	//				console.log('----------')
	param.success = function(res) {
		if(res.code == 1) {
			_success(res);

		} else if(res.code == 401) {
			mui.toast('请登录后操作');
			localStorage.removeItem('userInfo');
			App.navigatorTo({
				url: 'login.html', //String类型,要打开的界面的地址
				id: 'login', //String类型,要打开的界面的id
			})
		} else {
			param.fail ? param.fail(res) : mui.toast('系统错误 ' + res.msg);
		}
		plus.nativeUI.closeWaiting();
	}

	param.error = param.error || function(xhr, type, errorThrown) {
		console.log(type)
		plus.nativeUI.closeWaiting();
		mui.toast('服务器超时');
	}
	var task = mui.ajax(this.domain + this.aliasMap[url], param);
	return task;
}

Http.post = function(alias, param) {
	param.type = 'POST';
	this._prevRequest(alias, param, 'http');
}

Http.get = function(alias, param) {
	param.type = 'GET';
	//	console.log(JSON.stringify(param))
	this._prevRequest(alias, param, 'http');
}

Http.upload = function(param, alias) {
	alias = alias || 'image'
	this._prevRequest(alias, param, 'upload');
}

Http.init = function(handle) {
	var that = this;
	mui.ajax(HTTP_CONFIG.domain + HTTP_CONFIG.apiPrefix + HTTP_CONFIG.aliasMap.getTime, {
		dataType: 'json', //json
		type: 'GET',
		success: function(data) {
			if(data.status == 1) {
				var did = plus.device.uuid ? encodeURIComponent(plus.device.uuid) : '123456';
				var time = 'did=' + did + '&time=' + data.data.time + '';
				var secretKey = CryptoJS.enc.Utf8.parse(HTTP_CONFIG.secretKey);
				var encryptedData = CryptoJS.AES.encrypt(time, secretKey, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				});
				handle.call(that, encodeURIComponent(encryptedData))

			}
		}
	})
}

Http.constant = {
	requestDefaultSetting: {
		data: {},
		dataType: 'json',
		timeout: 5000,
		header: {},
		error: function(xhr, type, errorThrown) {
			plus.nativeUI.closeWaiting();
			mui.toast('服务器错误:' + type);
		}
	}
}
Http.domain = 'http://powerbank.imcyk.com';
Http.aliasMap = {
	getAroundChargePlace: '/api/v1/getAroundChargePlace',
	getCabinetByName: '/api/v1/getCabinetByName',
	sendSms: '/api/v1/sendSms',
	loginWithMobile: '/api/v1/loginWithMobile'
}